import { Task, Pagination, Filters, ApiResponse } from './types';
import { TaskStatus, TaskType } from './constants';

// 模拟任务数据
const mockTasks: Task[] = [
  {
    id: 'T111',
    type: TaskType.PREMIUM_PROJECT,
    name: '某交付服务项目立项111',
    initiator: '伍罡/80293464',
    initiateTime: '2025-04-27 16:05:06',
    currentStep: '项目审批',
    currentHandler: '李勇/80230544',
    status: TaskStatus.IN_PROGRESS
  },
  {
    id: 'T112',
    type: TaskType.PREMIUM_PROJECT,
    name: '某交付服务项目',
    initiator: '伍罡/80293464',
    initiateTime: '2025-04-25 09:37:58',
    currentStep: '业务审批',
    currentHandler: '伍罡/80293464',
    status: TaskStatus.IN_PROGRESS
  },
  {
    id: 'T113',
    type: TaskType.PREMIUM_PROJECT,
    name: '某交付服务项目',
    initiator: '伍罡/80293464',
    initiateTime: '2025-04-23 19:06:48',
    currentStep: '',
    currentHandler: '',
    status: TaskStatus.IN_PROGRESS
  },
  {
    id: 'BU11179',
    type: TaskType.BUSINESS_FEATURE,
    name: '数字产品',
    initiator: '伍罡/80293464',
    initiateTime: '2025-04-16 11:12:24',
    currentStep: '业务审批',
    currentHandler: '伍罡/80293464',
    status: TaskStatus.IN_PROGRESS,
    team: '协调办公开发团队'
  },
  {
    id: 'DEMOT0001-E33',
    type: TaskType.PROJECT_CHANGE,
    name: '探索专属服务',
    initiator: '伍罡/80293464',
    initiateTime: '2025-04-02 10:36:46',
    currentStep: '项目负责人审批',
    currentHandler: '万正彰/80374471',
    status: TaskStatus.IN_PROGRESS
  },
  {
    id: 'BU12781-E21',
    type: TaskType.PROJECT_CHANGE,
    name: '111',
    initiator: '伍罡/80293464',
    initiateTime: '2025-03-27 20:42:06',
    currentStep: '项目负责人审批',
    currentHandler: '谢汶沁/80231866',
    status: TaskStatus.IN_PROGRESS
  }
];

// 模拟分页信息
const mockPagination: Pagination = {
  currentPage: 1,
  totalItems: 315,
  pageSize: 8,
  totalPages: 40
};

// 模拟筛选选项
const mockFilters: Filters = {
  specialTopics: [
    { label: '全部', value: 'all' },
    { label: '专题特性', value: 'special' }
  ],
  statuses: [
    { label: '活跃中', value: 'active' },
    { label: '已结束', value: 'ended' },
    { label: '强行终止', value: 'terminated' }
  ]
};

/**
 * 获取任务列表数据
 * @param params 请求参数
 */
export const fetchTaskList = async (params: {
  page: number;
  pageSize: number;
  status?: string;
  specialTopic?: string;
  keyword?: string;
}): Promise<ApiResponse<{ tasks: Task[]; pagination: Pagination; filters: Filters }>> => {
  // 模拟API请求延迟
  return new Promise((resolve) => {
    setTimeout(() => {
      // 简单的筛选逻辑
      let filteredTasks = [...mockTasks];
      
      // 根据状态筛选
      if (params.status && params.status !== 'active') {
        filteredTasks = filteredTasks.filter(task => 
          params.status === 'ended' 
            ? task.status === TaskStatus.ENDED 
            : task.status === TaskStatus.TERMINATED
        );
      }
      
      // 根据专题特性筛选
      if (params.specialTopic && params.specialTopic !== 'all') {
        filteredTasks = filteredTasks.filter(task => 
          task.type === TaskType.BUSINESS_FEATURE || 
          task.type === TaskType.TECHNICAL_FEATURE
        );
      }
      
      // 根据关键词搜索
      if (params.keyword) {
        const keyword = params.keyword.toLowerCase();
        filteredTasks = filteredTasks.filter(task => 
          task.name.toLowerCase().includes(keyword) || 
          task.id.toLowerCase().includes(keyword) ||
          task.initiator.toLowerCase().includes(keyword)
        );
      }
      
      // 更新分页信息
      const pagination: Pagination = {
        ...mockPagination,
        currentPage: params.page,
        totalItems: filteredTasks.length,
        pageSize: params.pageSize,
        totalPages: Math.ceil(filteredTasks.length / params.pageSize)
      };
      
      // 根据分页截取数据
      const start = (params.page - 1) * params.pageSize;
      const end = start + params.pageSize;
      filteredTasks = filteredTasks.slice(start, end);
      
      resolve({
        success: true,
        data: {
          tasks: filteredTasks,
          pagination,
          filters: mockFilters
        }
      });
    }, 500);
  });
};

/**
 * 终止任务
 * @param taskId 任务ID
 */
export const terminateTask = async (taskId: string): Promise<ApiResponse<boolean>> => {
  // 模拟API请求延迟
  return new Promise((resolve) => {
    setTimeout(() => {
      console.log(`终止任务: ${taskId}`);
      resolve({
        success: true,
        data: true,
        message: '任务已终止'
      });
    }, 800);
  });
}; 